import { getStream } from '../Core/connect'
import { Material } from '../Material/Material'
import { Object3D } from './Object3D'
import * as MathUtils from '../Math/MathUtils'
import { Vector3 } from '../Math/Vector3'

export class Extrusion extends Object3D {
    readonly type: string
    baseHeight: number
    targetHeight: number
    constructor(name: string, baseHeight: number = 0, targetHeight: number = 2000) {
        super({name: name, type: 'Extrusion'})
        this.type = 'Extrusion'
        this.baseHeight = baseHeight
        this.targetHeight = targetHeight
        this.setBaseHight(baseHeight)
        this.setTargetHight(targetHeight)
    }
    setBaseHight(height: number) {
        this.baseHeight = height
        const stream = getStream()
        const str = {extrusionFunction: 'setExtrusionBaseHeight', ids: `${this.uuid}`, baseHeight:`${this.baseHeight}`}
        stream.emitUIInteraction(str)
    }

    setTargetHight(height: number) {
        this.targetHeight = height
        const stream = getStream()
        const str = {extrusionFunction: 'setExtrusionTargetHeight', ids: `${this.uuid}`, targetHeight:`${this.targetHeight}`}
        stream.emitUIInteraction(str)
    }

    setMaterial(material: Material) {
        const stream = getStream()
        const str = {extrusionFunction: 'setExtrusionColor', ids: `${this.uuid}`, Material: material}
        stream.emitUIInteraction(str)
    }

    setPlace() {
        const stream = getStream()
        const str = {extrusionFunction: 'setExtrusionPlace', ids: `${this.uuid}`, name:`${this.name}`}
        stream.emitUIInteraction(str)
    }

    build() {
        const stream = getStream()
        const str = {extrusionFunction: 'buildExtrusion', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
    }

    buildByList(list:Array<Vector3>) {
        const stream = getStream()
        const str = {extrusionFunction: 'buildPolygonByList', ids: `${this.uuid}`, pointList: list}
        stream.emitUIInteraction(str)
    }

    setClick(fun: Function) {
        const stream = getStream()
        const uuid = this.uuid
        const sessionId = MathUtils.generateUUID()
        const str = {extrusionFunction: 'setExtrusionClick', ids: `${this.uuid}`, sessionId: sessionId}
        stream.emitUIInteraction(str)
        const getExtrusionClick = (data: string) => {
            const returnData = JSON.parse(data)
            if (returnData.uuid === uuid) {
                fun()
            }
        }
        stream.addResponseEventListener(`extrusionClick${this.uuid}`, getExtrusionClick)
    }
}

